package cn.cheegon.blog.service.impl;

import cn.cheegon.blog.common.api.CommonPage;
import cn.cheegon.blog.common.exception.Asserts;
import cn.cheegon.blog.entity.dto.ProjectDto;
import cn.cheegon.blog.model.CmsEssay;
import cn.cheegon.blog.model.PmsProject;
import cn.cheegon.blog.mapper.PmsProjectMapper;
import cn.cheegon.blog.service.PmsProjectService;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 项目表 服务实现类
 * </p>
 *
 * @author cheegon
 * @since 2024-06-21
 */
@Service
public class PmsProjectServiceImpl extends ServiceImpl<PmsProjectMapper, PmsProject> implements PmsProjectService {

    @Override
    public CommonPage<ProjectDto> getList(String kwd,int pageNum, int pageSize) {
        QueryWrapper<PmsProject> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(kwd)){
            queryWrapper.like("title", kwd);
        }
        queryWrapper.orderByDesc("create_time");
        Page<PmsProject> page = this.baseMapper.selectPage(new Page<PmsProject>(pageNum, pageSize), queryWrapper);
        if (CollectionUtils.isEmpty(page.getRecords())){
            return CommonPage.emptyPage(page);
        }
        return CommonPage.restPage(page, ProjectDto.class);
    }

    @Override
    public ProjectDto getItem(Long id) {
        PmsProject byId = this.baseMapper.selectById(id);
        if (byId==null){
            Asserts.fail("不存在");
        }
        return BeanUtil.toBean(byId, ProjectDto.class);
    }

    @Override
    public void add(PmsProject dto) {
        this.baseMapper.insert(dto);
    }

    @Override
    public void edit(PmsProject dto) {
        this.baseMapper.updateById(dto);
    }

    @Override
    public void del(Long id) {
        this.baseMapper.deleteById(id);
    }
}
